package com.demo.Service.Impl;

import com.demo.Service.UserService;
import com.demo.common.MessageUtils;
import com.demo.entity.User;
import com.demo.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public MessageUtils<PageInfo<User>> SelectAll(int index, int number,String str) {

        if (!str.equals("")){
            PageHelper.startPage(index, number);
            List<User> users = userMapper.VagueSelect(str);
            PageInfo<User> pageInfo = new PageInfo<>(users);
            return new MessageUtils<>(100, "成功", pageInfo);
        }else {
            PageHelper.startPage(index, number);
            List<User> users = userMapper.Select();
            PageInfo<User> pageInfo = new PageInfo<>(users);
            return new MessageUtils<>(100, "成功", pageInfo);
        }
    }

    @Override
    public MessageUtils<Integer> Inserts(ArrayList<User> users) {
        userMapper.Inserts(users);
        return new MessageUtils<>(200,"成功");
    }

    @Override
    public MessageUtils<Integer> updateUser(User user) {
        int i = userMapper.updateUser(user);
        return new MessageUtils<>(i>0?200:500,i>0?"成功":"失败");

    }

    @Override
    public MessageUtils<?> deleteUser(String id) {
        int i = userMapper.deleteUser(id);
        return new MessageUtils<>(i>0?200:500,i>0?"删除成功":"删除失败");
    }

    @Override
    public MessageUtils<?> SelectUser(String username, String password) {
        if(!username.equals("") &&!password.equals("")){
          //注意，如果用户名重复，会导致查询出错，应该进行用户名校验，保证不重复
            User user = userMapper.SelectUser(username,password);
          if (user!=null){
              return new MessageUtils<>(200,"成功",user);
          }else {
              return new MessageUtils<>(400,"密码或用户名错误");
          }
        }else {
            return new MessageUtils<>(400,"密码或用户名不能为空");
        }
    }
}
